package com.google.code.japndroid;

import android.app.*;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import java.lang.Thread;
import android.os.SystemClock;
import android.util.Log;
import android.preference.PreferenceManager;

public class PacketKillerMasterSyncAutomaticallyService extends Service {

	public long ACTIVE_INTERVAL = 60 * 1000 * 5;
	public AlarmManager manager;
	public Thread t;
	private PendingIntent pi;
	                                    
	@Override
	public void onStart(Intent intent,int startId){
		Log.v("PACKET KILLER","SYNC SERVICE on Start");

    	
        long interval = 10 * 1000 * 60;
		SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		long wk = Long.parseLong(preference.getString("pref_itvl_value","0"));
        if( wk > 0L ){
        	interval = wk * 1000 * 60;
        }

		Intent nextIntent = new Intent(getBaseContext(),PacketKillerMasterSyncAutomaticallyService.class);
		//nextIntent.setClassName("com.google.code.japndroid","com.google.code.japndroid.PacketKillerMasterSyncAutomaticallyService");
		nextIntent.setAction("interval");

		long firstTime = SystemClock.elapsedRealtime();
		pi = PendingIntent.getService(this,0,nextIntent,0);
		manager = (AlarmManager) getSystemService(ALARM_SERVICE);
		manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime + interval ,pi);
		//manager.cancel(pi);
		//manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, interval, pi);
		
		Log.v("PACKET KILLER","SYNC CALL START() firstTime is " + firstTime +" / interval is " + interval);	
		if( ! "interval".equals(intent.getAction())) {
			Log.v("PACKET KILLER","ACTION is NOT interval,return");
			return;
		}
		
		Log.v("PACKET KILLER","Sync Start , action is " + intent.getAction() + " and Interval is " + interval);
		if("interval".equals(intent.getAction()) && interval > 1 ){
			t = new Thread(){
				@Override
				public void run(){
					Log.v("PACKET KILLER","S:######### Sync On " + (ACTIVE_INTERVAL  / 1000)  +" sec #########");
					if( ! android.content.ContentResolver.getMasterSyncAutomatically()){
						android.content.ContentResolver.setMasterSyncAutomatically(true);
						statusCallback();
					}
					try{
						Thread.sleep(ACTIVE_INTERVAL);	
						Log.v("PACKET KILLER","S:######### Sync Off #########");
						android.content.ContentResolver.setMasterSyncAutomatically(false);
						statusCallback();
					}catch (InterruptedException e){
						Log.v("PACKET KILLER","S:##### Thread STOPPED ####");
					}
				}
			};
			t.start();
		}
	}
	
	private void statusCallback(){

		// DONE intent を投げる
		Intent message = new Intent(PacketKillerConstants.MASTER_SYNCAUTOMATICALLY_CHANGE_DONE);
		getBaseContext().sendBroadcast(message);
	}

	@Override
	public void onCreate(){
		//initSensorManager(sensorEventListener);
	}
	@Override
	public void onDestroy(){
		if( manager != null  && pi != null){
			manager.cancel(pi);
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}